package dfs;

public class 优美的排列 {
    int res=0;
    public int countArrangement(int n) {
        if (n<2)
            return 1;
        int[] visited = new int[n+1];
        dfs(n,1,visited);
        return res;
    }
    private void dfs(int n, int index, int[] visited) {
        //索引下标大于n
        if (index>n){
            res++;
            return;
        }
        for (int i = 1; i <=n ; i++) {
            //表示第i个数字是否使用过
            if (visited[i]==0){
                visited[i]=1;
                if (i%index==0 || index%i==0)
                    dfs(n,index+1,visited);
                visited[i]=0;
            }
        }
    }
}
